﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Text;
using yncTech.Common;
using yncTech.BusinessObjects;

/// <summary>
///AdminBasePage 的摘要说明
/// </summary>
public class AdminBasePage : Page
{
    public AdminBasePage()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    public OperationType ActionType
    {
        get;
        set;
    }

    public string ActionCode
    {
        get;
        set;
    }

    protected override void OnPreInit(EventArgs e)
    {
        if (LoginUserInfo.CurrentUser == null)
        {
            WebHelper.ReLogin("/Admin/AdminLogin.aspx");
            return;
        }
        Theme = yncSettings.AdminTheme;
        Title = yncSettings.AdminTitle;
        base.OnPreInit(e);

    }

    protected override void OnLoad(EventArgs e)
    {
        if (Request.QueryString["type"] != null)
        {
            ActionCode = Request.QueryString["type"];
            switch (Request.QueryString["type"].ToUpper())
            {
                case "A":
                    ActionType = OperationType.Add;
                    break;
                case "U":
                    ActionType = OperationType.Update;
                    break;
                case "D":
                    ActionType = OperationType.Delete;
                    break;
                case "V":
                    ActionType = OperationType.View;
                    break;
                case "R":
                    ActionType = OperationType.Reply;
                    break;
                case "C":
                    ActionType = OperationType.Confirm;
                    break;
                case "B":
                    ActionType = OperationType.Return;
                    break;
                default:
                    ActionType = OperationType.None;
                    break;
            }
        }

        base.OnLoad(e);

    }


    protected string GetAddLink(string url, object urlValue, int width, int height)
    {
        return GetLink("新增", "A", url, urlValue, width, height, false, null);
        //return string.Format("<a class=\"style4\" href=\"javascript:OpenWindow('{0}','{1}','{2}',{3},{4});\">{5}</a>", "新增", "A", url, width, height, "新增");
    }
    protected string GetAddLink(string url, object urlValue, int width, int height, params string[] args)
    {
        return GetLink("新增", "A", url, urlValue, width, height, false, args);
        //return string.Format("<a class=\"style4\" href=\"javascript:OpenWindow('{0}','{1}','{2}',{3},{4});\">{5}</a>", "新增", "A", url, width, height, "新增");
    }


    protected string GetUpdateLink(string url, object urlValue, int width, int height, params string[] args)
    {
        return GetLink("修改", "U", url, urlValue, width, height, false, args);
    }

    protected string GetLink(string text, string type, string url, object urlValue, int width, int height, bool isSpecifyURL, params string[] args)
    {
        string preFix = GetFileFolder(isSpecifyURL);
        return WebHelper.GetLink(text, type, preFix + url, urlValue, width, height, args);
    }

    protected string GetDeleteLink(string url, object data, params string[] args)
    {
        return GetConfirmLink("删除", "D", url, data, "您确定要删除吗?", args);
    }

    protected string GetConfirmLink(string text, string type, string url, object data, string message, params string[] args)
    {
        return WebHelper.GetConfirmLink(text, type, url, data, message, args);
    }


    #region ext link
    protected string GetAddLinkExt(string url, string cssClass, object urlValue, int width, int height)
    {
        return GetLinkExt("新增",cssClass, "A", url, urlValue, width, height, false, null);
        //return string.Format("<a class=\"style4\" href=\"javascript:OpenWindow('{0}','{1}','{2}',{3},{4});\">{5}</a>", "新增", "A", url, width, height, "新增");
    }
    protected string GetAddLinkExt(string url,string cssClass, object urlValue, int width, int height, params string[] args)
    {
        return GetLinkExt("新增",cssClass, "A", url, urlValue, width, height, false, args);
        //return string.Format("<a class=\"style4\" href=\"javascript:OpenWindow('{0}','{1}','{2}',{3},{4});\">{5}</a>", "新增", "A", url, width, height, "新增");
    }


    protected string GetUpdateLinkExt(string url, string cssClass, object urlValue, int width, int height, params string[] args)
    {
        return GetLinkExt("修改",cssClass, "U", url, urlValue, width, height, false, args);
    }

    protected string GetLinkExt(string text,string cssClass, string type, string url, object urlValue, int width, int height, bool isSpecifyURL, params string[] args)
    {
        string preFix = GetFileFolder(isSpecifyURL);
        return WebHelper.GetLink(text,cssClass,false, type, preFix + url, urlValue, width, height, args);
    }

    protected string GetDeleteLinkExt(string url,string cssClass, object data, params string[] args)
    {
        return GetConfirmLinkExt("删除",cssClass, "D", url, data, "您确定要删除吗?", args);
    }

    protected string GetConfirmLinkExt(string text,string cssClass, string type, string url, object data, string message, params string[] args)
    {
        return WebHelper.GetConfirmLink(text,cssClass,false, type, url, data, message, args);
    }
    #endregion 


    protected string GetNewTabLik(string text, string type, string url, object data, params string[] args)
    {
        return WebHelper.GetNewTabLink(text, type, url, data, args);
    }

    private string GetFileFolder(bool isSpecifyURL)
    {
        if (isSpecifyURL)
            return string.Empty;
        else
        {
            StringBuilder url = new StringBuilder();
            for (int i = 0; i < Request.Url.Segments.Length - 1; i++)
            {
                url.Append(Request.Url.Segments[i]);
            }
            return url.ToString();
        }
    }

    protected string GetYesNo(object objStatus)
    {
        bool? bstatus = objStatus as bool?;
        if (bstatus.HasValue)
        {
            if (bstatus.Value)
            {
                return "是";
            }
        }
        return "否";
    }

    protected string GetActiveStatus(object objStatus)
    {
        return GetActiveStatus(objStatus, string.Empty);
    }
    protected string GetActiveStatus(object objStatus, string format)
    {
        string retValue = string.Empty;
        int? activeStatus = objStatus as int?;

        if (!activeStatus.HasValue)
        {
            activeStatus = objStatus as Int16?;
        }

        if (!activeStatus.HasValue)
        {
            bool? bstatus = objStatus as bool?;
            if (bstatus.HasValue)
            {
                if (bstatus.Value)
                {
                    activeStatus = (int)ActiveStatus.Active;
                }
                else
                {
                    activeStatus = (int)ActiveStatus.InActive;
                }
            }
        }

        if (activeStatus.HasValue)
        {
            switch ((ActiveStatus)activeStatus.Value)
            {
                case ActiveStatus.InActive:
                    if (string.IsNullOrEmpty(format))
                    {
                        retValue = "<font color='red'><b>未激活</b></font>";

                    }
                    else
                    {
                        retValue = "未激活";
                    }
                    break;
                case ActiveStatus.Active:
                    if (string.IsNullOrEmpty(format))
                    {
                        retValue = "<font color='green'><b>激活</b></font>";
                    }
                    else
                    {
                        retValue = "已激活";
                    }
                    break;
                case ActiveStatus.Locked:
                    if (string.IsNullOrEmpty(format))
                    {
                        retValue = "<font color='red'><b>锁定</b></font>";

                    }
                    else
                    {
                        retValue = "锁定";
                    }
                    break;
                default:
                    if (string.IsNullOrEmpty(format))
                    {
                        retValue = "<font color='red'><b>未知</b></font>";

                    }
                    else
                    {
                        retValue = "未知";
                    }
                    break;
            }
        }
        else
        {
            return "<font color='red'><b>未知</b></font>";
        }
        if (!string.IsNullOrEmpty(format))
        {
            return string.Format(format, retValue);
        }
        return retValue;

    }

    protected string GetYesOrNo(object obj)
    {
        bool? bstatus = obj as bool?;

        if (bstatus.HasValue)
        {
            if (bstatus.Value)
            {
                return "<font color='Green'><b>是</b></font>";
            }
            else
            {

                return "<font color='red'><b>否</b></font>";
            }
        }
        return "<font color='red'>未知</font>";
    }

    protected string GetOpenClose(object obj)
    {
        bool? bstatus = obj as bool?;

        if (bstatus.HasValue)
        {
            if (bstatus.Value)
            {
                return "<font color='Green'>开放</font>";
            }
            else
            {

                return "<font color='red'>关闭</font>";
            }
        }
        return "<font color='red'>未知</font>";
    }

    protected string GetLendStatus(object obj)
    {
        int? status = obj as int?;

        if (status.HasValue)
        {
            if (status == 0)
            {
                return "<font>待借阅</font>";
            }
            else if (status == 1)
            {
                return "<font color='red'>借阅中</font>";
            }
            else if (status == 2)
            {
                return "<font color='Green'>已归还</font>";

            }

        }
        return "";
    }




    protected bool IsAdmin
    {
        get
        {
            if (LoginUserInfo.CurrentUser != null)
            {
                return LoginUserInfo.CurrentUser.IsAdmin;
            }
            return false;
        }
    }

    /// <summary>
    /// 是否为财务中心人员
    /// </summary>
    protected bool IsSzFin
    {
        get
        {
            if (LoginUserInfo.CurrentUser != null)
            {
                return LoginUserInfo.CurrentUser.ThirdUnitID == 1;
            }
            return false;
        }
    }

    protected string GetBoxNo(decimal boxID)
    {
        VBoxReceipts vb = new VBoxReceipts();
        vb.Query.Where(vb.Query.BoxID == boxID);
        vb.Query.es.Top =1;
        vb.Query.Load();

        if (vb.es.HasData)
        {
            return vb.BoxYear + vb.ReceiptCount.ToString().PadLeft(3, '0') + vb.BoxIndex.ToString().PadLeft(3, '0');
        }
        return string.Empty;
    }
}